package info.mhylle.mediaplayer.playlist;

import helliker.id3.MP3Comparator;
import helliker.id3.MP3File;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import java.util.*;

@Scope(org.jboss.seam.ScopeType.SESSION)
@Name("playlist")
public class Playlist {
    List<MP3File> fileList;
    private String name;
    private String type;
    private int position = 0;
    private BitSet bitSet;

    public Playlist() {
        fileList = new ArrayList<MP3File>();
    }

    public void addMp3(MP3File file) {
        fileList.add(file);
    }

    public List<MP3File> getList() {
        Collections.sort(fileList, new MP3Comparator());
        return fileList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }


    public int getRandomPosition() {
        if (bitSet == null) {
            bitSet = new BitSet(getList().size());
        }
        if (bitSet.isEmpty()) {
            int randomnr = new Random().nextInt(getList().size());
            bitSet.set(randomnr);
            return randomnr;
        } else {
            if (bitSet.cardinality() == getList().size() - 1) {
                return -1;
            }
            int randomnr = new Random().nextInt(getList().size());
            while (bitSet.get(randomnr)) {
                randomnr = new Random().nextInt(getList().size());
            }
            bitSet.set(randomnr);
            return randomnr;
        }
    }
}
